package fyf.com.carddesign.main;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.PhotoView;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindInt;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import fyf.com.carddesign.BaseActivity;
import fyf.com.carddesign.Bll.UserBll;
import fyf.com.carddesign.MyUtl.MyUtl;
import fyf.com.carddesign.R;

/**
 * Created by yxz on 2018/10/30.
 */

public class CarToonActivity extends BaseActivity {
    @BindView(R.id.cc_vp)
    ViewPager _vp;
    @BindView(R.id.seekBar)
    SeekBar _skb;
    @BindView(R.id.cc_tv_tip)
    TextView _tvTip;
    @BindView(R.id.cc_tv_title)
    TextView _tvTitle;
    @BindView(R.id.cc_rl_anim)
    RelativeLayout _rlAnim;
    private int resumePosition, totalCount;
    String tmpLocalPath;//上层选中的文件路径地址
    Bundle _bundle;
    String allPath;//全路径
    public static final String RESUME_DATA = "yang";
    SharedPreferences sp;
    SharedPreferences.Editor edSp;
    List<String> list;
    private boolean isOpenOrClose = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        _bundle = getIntent().getExtras();
        tmpLocalPath = _bundle.getString(MyUtl.PATH);
        if (tmpLocalPath.contains("幽州")) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        initView();
        iniData();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initView() {
        allPath = MyUtl.SD_HOME_DIR + tmpLocalPath;
        list = new ArrayList<>();
        sp = getSharedPreferences(RESUME_DATA, MODE_PRIVATE);
        edSp = sp.edit();
        _skb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                _vp.setCurrentItem(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    private void iniData() {
        list = UserBll.getInstance(this).getAllFilePath(tmpLocalPath);
        _skb.setMax(list.size());
        /**
         * 此方式是可以复用view的，不会造成内存极具的增加。是一种很好的方式
         */
        _vp.setAdapter(new PagerAdapter() {
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                PhotoView iv = new PhotoView(CarToonActivity.this);
                Glide.with(CarToonActivity.this).load(allPath + list.get(position)).into(iv);
                iv.setOnClickListener(v -> {
                    if (isOpenOrClose) {
                        _tvTitle.setVisibility(View.GONE);
                        _rlAnim.setVisibility(View.GONE);
                        isOpenOrClose = false;
                    } else {
                        _tvTitle.setVisibility(View.VISIBLE);
                        _rlAnim.setVisibility(View.VISIBLE);
                        isOpenOrClose = true;
                    }
                });
                container.addView(iv);
                return iv;
            }

            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
        });
        _vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                resumePosition = position;
                _skb.setProgress(position);
                _tvTitle.setText(list.get(position));
                _tvTip.setText(position + 1 + "/" + totalCount);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        totalCount = list.size();
        int tmpPositon = sp.getInt(tmpLocalPath, 0);
        _vp.setCurrentItem(tmpPositon);
    }

    @Override
    public int getLayoutId() {
        return R.layout.cartoon_common;
    }

    @Override
    protected void onStop() {
        super.onStop();
        edSp.putInt(tmpLocalPath, resumePosition);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        edSp.commit();
    }


}
